<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>用户列表</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="../../css/oksub.css">
	<script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
	<!--模糊搜索区域-->
	<div class="layui-row">
		<form class="layui-form ok-search-form">
			<div class="layui-form-item">
<!--				<div class="layui-inline">-->
<!--					<label class="layui-form-label">开始日期</label>-->
<!--					<div class="layui-input-inline">-->
<!--						<input type="text" class="layui-input" placeholder="开始日期" autocomplete="off" id="startTime" name="startTime">-->
<!--					</div>-->
<!--				</div>-->
<!--				<div class="layui-inline">-->
<!--					<label class="layui-form-label">截止日期</label>-->
<!--					<div class="layui-input-inline">-->
<!--						<input type="text" class="layui-input" placeholder="截止日期" autocomplete="off" id="endTime" name="endTime">-->
<!--					</div>-->
<!--				</div>-->
<!--				<div class="layui-inline">-->
<!--					<label class="layui-form-label">账号</label>-->
<!--					<div class="layui-input-inline">-->
<!--						<input type="text" class="layui-input" placeholder="账号" autocomplete="off" name="username">-->
<!--					</div>-->
<!--				</div>-->
				<div class="layui-inline">
					<label class="layui-form-label">姓名</label>
					<div class="layui-input-inline">
						<input type="text" class="layui-input" placeholder="姓名" autocomplete="off" name="emp_name">
					</div>
				</div>
<!--				<div class="layui-inline">-->
<!--					<label class="layui-form-label">昵称</label>-->
<!--					<div class="layui-input-inline">-->
<!--						<input type="text" class="layui-input" placeholder="昵称" autocomplete="off" name="nickname">-->
<!--					</div>-->
<!--				</div>-->
<!--				<div class="layui-inline">-->
<!--					<label class="layui-form-label">邮箱</label>-->
<!--					<div class="layui-input-inline">-->
<!--						<input type="text" class="layui-input" placeholder="邮箱" autocomplete="off" name="email">-->
<!--					</div>-->
<!--				</div>-->
<!--				<div class="layui-inline">-->
<!--					<label class="layui-form-label">请选择角色</label>-->
<!--					<div class="layui-input-inline">-->
<!--						<select name="role" lay-verify="" lay-search>-->
<!--							<option value="" selected>请选择角色</option>-->
<!--							<option value="0">超级会员</option>-->
<!--							<option value="1">普通用户</option>-->
<!--						</select>-->
<!--					</div>-->
<!--				</div>-->
<!--				<div class="layui-inline">-->
<!--					<label class="layui-form-label">请选择状态</label>-->
<!--					<div class="layui-input-inline">-->
<!--						<select name="status" lay-verify="" lay-search>-->
<!--							<option value="" selected>请选择状态</option>-->
<!--							<option value="0">启用</option>-->
<!--							<option value="1">停用</option>-->
<!--						</select>-->
<!--					</div>-->
<!--				</div>-->
				<div class="layui-inline">
					<div class="layui-input-inline">
						<button class="layui-btn" lay-submit="" lay-filter="search">
							<i class="layui-icon">&#xe615;</i>
						</button>
					</div>
				</div>
			</div>
		</form>
	</div>
	<!--数据表格-->
	<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
</div>
<!--js逻辑-->
<script src="../../lib/layui/layui.js"></script>
<script>
	layui.use(["element", "jquery", "table", "form", "laydate", "okLayer", "okUtils", "okMock"], function () {
		let table = layui.table;
		let form = layui.form;
		let laydate = layui.laydate;
		let okLayer = layui.okLayer;
		let okUtils = layui.okUtils;
		let okMock = layui.okMock;
		let $ = layui.jquery;

		okLoading.close($);

		laydate.render({elem: "#startTime", type: "datetime"});
		laydate.render({elem: "#endTime", type: "datetime"});

		let userTable = table.render({
			elem: '#tableId',
			url: "../../../empServlet",
			limit: 20,
			page: true,
			toolbar: true,
			toolbar: "#toolbarTpl",
			size: "sm",
			cols: [[
				{type: "checkbox", fixed: "left"},
				{field: "emp_id", title: "ID", width: 160, sort: true},
				{field: "emp_name", title: "姓名", width: 100},
				{field: "emp_sex", title: "性别", width: 100},
				{field: "emp_birthday", title: "生日", width: 100},
				{field: "emp_data", title: "入职日期", width: 100},
				{field: "emp_salaty", title: "工资", width: 100, templet: "#roleTpl"},
				{field: "emp_id", title: "部门编号", width: 100, templet: "#statusTpl"},
				{field: "dept_id", title: "部门id", width: 200},
				{field: "dept_name", title: "部门", width: 120},
				{title: "操作", width: 100, align: "center", fixed: "right", templet: "#operationTpl"}
			]],
			done: function (res, curr, count) {
				console.info(res, curr, count);
			}
		});

		form.on("submit(search)", function (data) {
			console.log(data);
			userTable.reload({
				where: data.field,
				page: {curr: 1}
			});
			return false;
		});

		table.on("toolbar(tableFilter)", function (obj) {
			switch (obj.event) {
				case "batchEnabled":
					batchEnabled();
					break;
				case "batchDisabled":
					batchDisabled();
					break;
				case "batchDel":
					batchDel();
					break;
				case "add":
					add();
					break;
			}
		});

		table.on("tool(tableFilter)", function (obj) {
			let data = obj.data;
			switch (obj.event) {
				case "edit":
					edit(data);
					break;
				case "del":
					del(data.emp_id);
					break;
			}
		});

		function batchEnabled() {
			okLayer.confirm("确定要批量启用吗？", function (index) {
				layer.close(index);
				let idsStr = okUtils.tableBatchCheck(table);
				if (idsStr) {
					okUtils.ajax("/user/normalUser", "put", {idsStr: idsStr}, true).done(function (response) {
						console.log(response);
						okUtils.tableSuccessMsg(response.msg);
					}).fail(function (error) {
						console.log(error)
					});
				}
			});
		}

		function batchDisabled() {
			okLayer.confirm("确定要批量停用吗？", function (index) {
				layer.close(index);
				let idsStr = okUtils.tableBatchCheck(table);
				if (idsStr) {
					okUtils.ajax("/user/stopUser", "put", {idsStr: idsStr}, true).done(function (response) {
						console.log(response);
						okUtils.tableSuccessMsg(response.msg);
					}).fail(function (error) {
						console.log(error)
					});
				}
			});
		}

		function batchDel() {
			okLayer.confirm("确定要批量删除吗？", function (index) {
				layer.close(index);
				let emp_id = okUtils.tableBatchCheck(table);
				if (emp_id) {
					okUtils.ajax("../../../empServlet?type=dels", "get", {emp_ids: emp_id}, true).done(function (response) {
						console.log(response);
						okUtils.tableSuccessMsg(response.msg);
					}).fail(function (error) {
						console.log(error)
					});
				}
			});
		}

		function add() {
			okLayer.open("添加用户", "user-insert.html", "90%", "90%", null, function () {
				userTable.reload();
			})
		}

		function edit(data) {
			okLayer.open("更新用户", "user-update.html", "90%", "90%", function (layero) {
				let iframeWin = window[layero.find("iframe")[0]["name"]];
				iframeWin.initForm(data);
			}, function () {
				userTable.reload();
			})
		}

		function del(id) {
			okLayer.confirm("确定要删除吗？", function () {
				okUtils.ajax("../../../empServlet", "get", {emp_id: id,type:"del"}, true).done(function (response) {
					console.log(response);
					okUtils.tableSuccessMsg(response.msg);
				}).fail(function (error) {
					console.log(error)
				});
			})
		}
	})
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="batchEnabled">批量启用</button>
		<button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="batchDisabled">批量停用</button>
		<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDel">批量删除</button>
		<button class="layui-btn layui-btn-sm" lay-event="add">添加用户</button>
	</div>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
	<a href="javascript:" title="编辑" lay-event="edit"><i class="layui-icon">&#xe642;</i></a>
	<a href="javascript:" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a>
</script>

<script type="text/html" id="statusTpl">
	{{#  if(d.status == 0){ }}
	<span class="layui-btn layui-btn-normal layui-btn-xs">启用</span>
	{{#  } else if(d.status == 1) { }}
	<span class="layui-btn layui-btn-warm layui-btn-xs">停用</span>
	{{#  } }}
</script>

<script type="text/html" id="roleTpl">
	{{#  if(d.role == 0){ }}
	<span class="layui-btn layui-btn-normal layui-btn-xs">超级会员</span>
	{{#  } else if(d.role == 1) { }}
	<span class="layui-btn layui-btn-warm layui-btn-xs">普通用户</span>
	{{#  } }}
</script>
</body>
</html>
